Namespaces in ST: declaration

Syntax
NAMESPACE name
 
( * optional_begin *) USING Namespace_2;
USING Namespace_3; (* optional_end *)
 
(* user-defined declaration of POU and/or data types *)
 
END_NAMESPACE
Meaning

declaration of a →namespace, name must be an →IEC-identifier (see example 1 below) or a fully qualified name
A fully qualified name consists of a sequence of namespace identifiers separated by . (dots). This permits the declaration of nested namespaces without having to repeat the keywords NAMESPACE ... END_NAMESPACE for each namespace (see example 3 below).

A namespace can contain the following language elements:

It is also possible to declare namespaces (and the above language elements in them) within the ST-interface of a C-/C++-block.

Restrictions

  • The →IEC-standard lists other language elements for a namespace as well. These elements are not supported.

  • Access specifiers for namespaces (e.g. INTERNAL) are not supported.

If you declare language elements in a namespace, you can sure use them outside of this namespace as well.
(info) The USING namespace directive after the name of the function block is optional. Use it to access the language elements of the specified namespace (see "Namespaces in ST: usage"  for details on this directive).
Observe: If you specify a namespace within the global-object, this namespace has no impact onto the access to such global variables by using the appertaining →external variable. Because the access is done via the name of the variable. Hence, no fully qualified name and no additional USING namespace directive is required for an external variable, if a global variable in a global-object with a namespace is accessed.

(info) It is also possible to specify a namespace when creating certain ST language elements (see "Creating application in ST") or within the application navigator (see "Actions with folders/objects in context with namespaces").

Example 1: Declaration of a namespace

NAMESPACE N1 (* starting the declaration of the namespace 'N1' *)
  FUNCTION F1
  // ... declaration
  END_FUNCTION
 
  TYPE
    T1: STRUCT
      Elem1 : BOOL;
      // ... and more declaration
    END_STRUCT;
  END_TYPE
 
  FUNCTION_BLOCK FB1
  // ... declaration
  END_FUNCTION_BLOCK
 
END_NAMESPACE (* ending the declaration of the namespace 'N1' *)
Example 2: Declaration of nested namespaces (with keywords repeated)
NAMESPACE Standard (* starting the declaration of the namespace 'Standard' *)
  NAMESPACE Timers (* starting the declaration of the nested namespace 'Standard.Timers' *)
    NAMESPACE HighResolution (* starting the declaration of the nested namespace 'Standard.Timers.HighResolution' *)
 
      FUNCTION TimeTick: DWORD       (* 'TimeTick' is included in the namespace 'Standard.Timers.HighResolution'; compare examples 3 and 4. *)
      // ... declaration
      END_FUNCTION
 
    END_NAMESPACE (* ending the declaration of the nested namespace 'Standard.Timers.HighResolution' *)
 
    TYPE
      LOCAL_TIME: STRUCT
        Elem1 : BOOL;
        // ... and more declaration
      END_STRUCT;
    END_TYPE
 
    FUNCTION_BLOCK TON
      VAR_INPUT
        In : BOOL;
        PT : TIME;
      END_VAR
      // ...  and more declaration
    END_FUNCTION_BLOCK
 
  END_NAMESPACE (* ending the declaration of the nested namespace 'Standard.Timers' *)
  NAMESPACE Counters  (* starting the declaration of the nested namespace 'Standard.Counters' *)
    FUNCTION_BLOCK CUP
    // ... declaration
    END_FUNCTION_BLOCK
 
    FUNCTION_BLOCK CDOWN
    // ... declaration
    END_FUNCTION_BLOCK
  END_NAMESPACE (* ending the declaration of the nested namespace 'Standard.Counters' *)
 
END_NAMESPACE (* ending the declaration of the namespace 'Standard' *)

Example 3: Declaration of a nested namespace (with a fully qualified name)

NAMESPACE Standard.Timers.HighResolution
 
  FUNCTION TimeResolution: DWORD      (* 'TimeResolution' is included in the namespace 'Standard.Timers.HighResolution'; compare examples 2 and 4. *)
  // ... declaration
  END_FUNCTION
END_NAMESPACE
Example 4: Declaration of nested namespaces (combination of example 2 and 3)
NAMESPACE Standard.Timers
 
  NAMESPACE HighResolution
 
    FUNCTION TimeLimit: DWORD       (* 'TimeLimit' is included in the namespace 'Standard.Timers.HighResolution'; compare examples 2 and 3. *)
    // ... declaration
    END_FUNCTION
 
  END_NAMESPACE
END_NAMESPACE